<?php
namespace app\common\service\impl;
use app\common\model\Contract;
use app\common\model\Rent;
use app\common\service\RentService;
use think\facade\Log;
class RentServiceImpl implements RentService
{
   
    public function save(array $data){	
        
       	$time = date("Y-m-dH:i:s",time());
        $string = str_replace(["-",":"],'',$time);
        $string.= $data['formValidate']['user_id'];
        $string.= mt_rand(100000, 999999);
        $string.= $data['formValidate']['user_id'];
        
        $saveData = [];
        
        foreach ($data['cars'] as $value){
            $value['expire_time'] = $data['formValidate']['expire_time'];
            $value['contract_no'] = $string;
            $value['user_id'] = $data['formValidate']['user_id'];
            $value['dispatch_status'] = 1;
            $saveData[] = $value;		
        }
        
        $contractModel = new Contract;  
        $rentModel = new Rent;
        
        try {
            $contractModel->startTrans();
            $contract = $contractModel->allowField(true)->saveAll($saveData);
            if($contract){
                $data['formValidate']['contract_no'] = $string;
                $data['formValidate']['contract_status'] = 1;
                $rent = $rentModel->allowField(true)->save($data['formValidate']);
                if($rent){            
                     $contractModel->commit();
                     return true;
                   
                }else{
                    $contractModel->rollback();
                    return false;
                }
            }
        } catch (\Exception $e) {
            $contractModel->rollback();
            Log::record('[ 合同表插入数据 ]'.$e->getMessage(),'error');
            return false;
        }  
    }
    
    public function index(array $where,int $page,int $length){
        try {
            $model = Rent::with('agency,user,contract')->where($where)->order('id', 'desc')
            ->paginate($length,false,[
                'type'     => 'bootstrap',
                'var_page' => 'page',
                'page'=>$page,
            ]);
           
        } catch (\Exception $e) {
            Log::record('长租列表:'.$e->getMessage(),'error');
            return false;
        }
        return $model;
    }
    public function details(String $num){
        $data = [];
        try {
            $data['contract'] = Contract::with('brand')->where(['contract_no'=>$num])->select();
        } catch (\Exception $e) {
            Log::record('[ 合同表查询数据 ]'.$e->getMessage(),'error');
            return false;
        }
        try {
            $data['rent'] = Rent::with('agency,user')->where(['contract_no'=>$num])->find();
        } catch (\Exception $e) {
            Log::record('[ 长租表查询数据 ]'.$e->getMessage(),'error');
            return false;
        }
        
        return $data;
    }
    
    
    public function dispatch(Array $data){
    	$data['dispatch_status'] = 2;
    	$contractModel = new Contract;  
    	$rentModel = new Rent;
    	try{
    		$contractModel->allowField(true)->save($data,['contract_no' => $data['num']]);
    		$rentModel->allowField(true)->save($data,['contract_no' => $data['num']]);
    	}catch(\Exception $e){
    		Log::record('[ 插入发车数据 ]'.$e->getMessage(),'error');
            return false;
    	}
    	
    	return true;
    }
    


    

}

